package com.aiju.cash.ui.member;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.MemberApi;
import com.aiju.cash.ui.member.entity.MemberCard;
import com.aiju.common.ActivityStack;
import com.aiju.utils.T;
import com.aiju.widgit.dialog.MemberDialog;
import com.aiju.widgit.dialog.SimpleDialog;
import com.aiju.widgit.layout.SettingItemTextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2017-02-20 15:59
 * @version: V1.0
 * @Description: 会员卡详情
 */
public class MemberCardInfoActivity extends BaseTitleActivity implements View.OnClickListener {

    private SettingItemTextView nameTextView, discountTextView, stateTextView;
    private SettingItemTextView numTextView, timeTextView;
    private TextView infoTextView;

    private String memberCardId;

    private MemberCard memberCard;

    private TextView changeTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_card_info);
        setCenterTitle("会员卡详情");
        setRightTitle("删除本卡");

        memberCardId = getIntent().getStringExtra("memberCardId");
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SimpleDialog(MemberCardInfoActivity.this)
                        .setTitileText("确认删除会员卡")
                        .setLayoutParams()
                        .setOnSimpleClick(new SimpleDialog.OnSimpleClick() {
                            @Override
                            public void onCancel() {
                            }

                            @Override
                            public void onSubmit() {
                                deleteMemberCard();
                            }
                        }).show();

            }
        });

        nameTextView = (SettingItemTextView) findViewById(R.id.tv_member_name);
        discountTextView = (SettingItemTextView) findViewById(R.id.tv_member_discount);
        stateTextView = (SettingItemTextView) findViewById(R.id.tv_member_state);
        numTextView = (SettingItemTextView) findViewById(R.id.tv_member_get);
        timeTextView = (SettingItemTextView) findViewById(R.id.tv_member_time);
        infoTextView = (TextView) findViewById(R.id.ev_member_info);
        changeTextView = (TextView) findViewById(R.id.btn_card_use);
        findViewById(R.id.tv_member_get).setOnClickListener(this);
        findViewById(R.id.btn_card_use).setOnClickListener(this);
        findViewById(R.id.btn_card_qr).setOnClickListener(this);

        requsetMemberCard();
    }

    /**
     * 修改状态
     */
    private void updateMemberCard(final String state) {
        mDialogHelper.showLoadingProgressDialog();
        new MemberApi().updateCard(memberCardId, state, mUser.getToken(), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                mDialogHelper.dismissProgressDialog();
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                mDialogHelper.dismissProgressDialog();
                DataJson dataJson = new DataJson(response);
                if (dataJson.isSuccess()) {
                    T.ToastShort("修改成功");
                    if (memberCard != null) {
                        memberCard.setStatus(state);
                        setMemberCardView();
                    }
                } else {
                    T.ToastShort(dataJson.getMsg());
                }
            }
        });
    }

    /**
     * 删除会员卡
     */
    private void deleteMemberCard() {
        mDialogHelper.showLoadingProgressDialog();
        new MemberApi().memberCardDelete(memberCardId, mUser.getToken(), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                mDialogHelper.dismissProgressDialog();
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                mDialogHelper.dismissProgressDialog();
                DataJson dataJson = new DataJson(response);
                if (dataJson.isSuccess()) {
                    T.ToastShort("删除成功");
                    ActivityStack.finish(ActivityStack.findActivity(MemberCardActivity.class.getName()));
                    startActivity(MemberCardActivity.class);
                } else {
                    T.ToastShort(dataJson.getMsg());
                }
            }
        });
    }

    /**
     * 请求会员卡详情
     */
    private void requsetMemberCard() {
        mDialogHelper.showLoadingProgressDialog();
        new MemberApi().memberCardInfo(memberCardId, mUser.getToken(), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                mDialogHelper.dismissProgressDialog();
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                mDialogHelper.dismissProgressDialog();
                DataJson dataJson = new DataJson(response);
                if (dataJson.isSuccess()) {
                    memberCard = new Gson().fromJson(dataJson.getData().toString(), new TypeToken<MemberCard>() {
                    }.getType());
                    setMemberCardView();
                } else {
                    T.ToastShort(dataJson.getMsg());
                }
            }
        });
    }

    private void setMemberCardView() {
        //会员卡状态1:生效2:失效 3：尚未生效 4：禁用
        if (memberCard.getStatus().equals("1")) {
            stateTextView.getRightView().setTextColor(Color.parseColor("#1eab75"));
        } else if (memberCard.getStatus().equals("2")) {
            stateTextView.getRightView().setTextColor(Color.parseColor("#666666"));
        } else if (memberCard.getStatus().equals("3")) {
            stateTextView.getRightView().setTextColor(Color.parseColor("#666666"));
        } else if (memberCard.getStatus().equals("4")) {
            stateTextView.getRightView().setTextColor(Color.parseColor("#ec4545"));
        }
        String exchange = getResources().getString(R.string.exchange_txt_hint);
        numTextView.getleftView().setText(Html.fromHtml(String.format(exchange, memberCard.getNum())));

        nameTextView.setRightText(memberCard.getName());
        discountTextView.setRightText(memberCard.getDiscount());
        stateTextView.setRightText(memberCard.getStatuText());

        //未过期 生效
        if (memberCard.getStatus().equals("1")) {
            changeTextView.setText("禁用本卡");
            //未过期 禁用
        } else if (memberCard.getStatus().equals("4")) {
            changeTextView.setText("启用本卡");
        } else {
            changeTextView.setText("启用本卡");
            changeTextView.setTextColor(Color.parseColor("#4dffffff"));
            changeTextView.setEnabled(false);
        }

        if (memberCard.isForever()) {
            timeTextView.setRightText("无限期");
        } else {
            timeTextView.setRightText(memberCard.getTime());
        }
        infoTextView.setText(memberCard.getNotice());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_member_get:
                goMemberCardUserActivity();
                break;
            case R.id.btn_card_use:
                goMemberCardUseActivity();
                break;
            case R.id.btn_card_qr:
                goMemberCardQrActivity();
                break;
        }
    }

    private void goMemberCardQrActivity() {
        Intent intent = new Intent(this, MemberCardQRActivity.class);
        intent.putExtra("memberCard", memberCard);
        startActivity(intent);
    }

    private void goMemberCardUseActivity() {
        if (changeTextView.getText().equals("禁用本卡")) {
            new MemberDialog(this)
                    .setLayoutParams()
                    .setTitileText("确认禁用本卡")
                    .setMsgText("禁用后，领取本卡的会员将不再享受相应的优惠。由此可能造成的损失与本公司无关")
                    .setLeftText("取消")
                    .setRightText("禁用")
                    .setOnSimpleClick(new MemberDialog.OnSimpleClick() {
                        @Override
                        public void onCancel() {
                        }

                        @Override
                        public void onSubmit() {
                            updateMemberCard("4");
                        }
                    }).show();
        } else {
            new MemberDialog(this)
                    .setLayoutParams()
                    .setTitileText("确认启用本卡")
                    .setMsgText("启用后，领取本卡的会员将继续享受相应的优惠")
                    .setLeftText("取消")
                    .setRightText("启用")
                    .setOnSimpleClick(new MemberDialog.OnSimpleClick() {
                        @Override
                        public void onCancel() {
                        }

                        @Override
                        public void onSubmit() {
                            updateMemberCard("1");
                        }
                    }).show();
        }
    }


    /**
     * 跳转会员卡领取名单
     */
    private void goMemberCardUserActivity() {
        Intent intent = new Intent(this, MemberCardUserActivity.class);
        intent.putExtra("memberCard", memberCard);
        startActivity(intent);
    }
}
